class CompetitionsController < ApplicationController
  before_filter:authenticate_user!
  
  # GET /competitions
  # GET /competitions.json
  def index
    @competitions = Competition.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @competitions }
    end
  end

  # GET /competitions/1
  # GET /competitions/1.json
  def show
    @competition = params[:id].nil? ? Competition.last : Competition.find_by(url: params[:id])
    
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @competition }
    end
  end

  # GET /competitions/new
  # GET /competitions/new.json
  def new
    @competition = Competition.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @competition }
    end
  end

  # GET /competitions/1/edit
  def edit
    @competition = Competition.find(params[:id])
  end

  # POST /competitions
  # POST /competitions.json
  def create
    @competition = Competition.new(params[:competition])

    respond_to do |format|
      if @competition.save
        format.html { redirect_to @competition, notice: 'Competition was successfully created.' }
        format.json { render json: @competition, status: :created, location: @competition }
      else
        format.html { render action: "new" }
        format.json { render json: @competition.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /competitions/1
  # PUT /competitions/1.json
  def update
    @competition = Competition.find(params[:id])

    respond_to do |format|
      if @competition.update_attributes(params[:competition])
        format.html { redirect_to @competition, notice: 'Competition was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @competition.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /competitions/1
  # DELETE /competitions/1.json
  def destroy
    render nothing: true, status: 404
    # @competition = Competition.find(params[:id])
    # @competition.destroy

    # respond_to do |format|
    #   format.html { redirect_to competitions_url }
    #   format.json { head :no_content }
    # end
  end

  # moved to POST /topic
  # POST /competitions/1/topic
  def topic
    @competition = Competition.find(params[:id])

    @topic = Topic.new(params[:topic])
    @remark = Remark.new(params[:remark])
    
    @topic.author = current_user
    @remark.author = current_user

    @remark.topic = @topic
    @topic.competition = @competition

    @topic.save
    @remark.save

    respond_to do |format|
      format.html { redirect_to @competition }
    end
  end

  # POST /competitions/1/question_list
  def question_list
    @competition = Competition.find(params[:id])
    @question_list = QuestionList.new(params[:qustion_list])

    @question_list.competition = @competition
    @question_list.author = current_user
    @question_list.save

    respond_to do |format|
      format.html { redirect_to @question_list }
    end
  end
end
